#-*- coding: utf8 -*-

import pygtk
pygtk.require('2.0')
import gtk

# threading handling
import sys, threading, gobject
if sys.platform == "win32":
    gobject.threads_init()
else:
    gtk.gdk.threads_init()

import tree, comparer


# FIXME: usar la que corresponde
class StatusBar(gtk.Statusbar):
    '''All status bar functionality.'''
    def __init__(self):
        gtk.Statusbar.__init__(self)
        self._context = self.get_context_id("unique_sb")
        self.show()
        self._timer = None
        
    def __call__(self, msg, timeout=3):
        if self._timer is not None:
            self._timer.cancel()
        self.push(self._context, msg)
        self._timer = threading.Timer(timeout, self.clear, ())
        self._timer.start()

    def clear(self):
        self.push(self._context, "")
        if self._timer is not None:
            self._timer.cancel()
            self._timer = None

ui_menu = """
<ui>
  <menubar name="MenuBar">
    <menu action="MainMenu">
      <menuitem action="NuevaComp"/>
      <menuitem action="Refrescar"/>
      <separator name="s1"/>
      <menuitem action="Salir"/>
    </menu>
    <menu action="AyudaMenu">
      <menuitem action="Ayuda"/>
      <menuitem action="AcercaDe"/>
    </menu>
  </menubar>
  <toolbar name="Toolbar">
    <toolitem action="NuevaComp"/>
    <toolitem action="Refrescar"/>
  </toolbar>
</ui>
"""

class MainWindow(object):
    def delete(self, widget, event=None):
        gtk.main_quit()
        return False

    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_icon_from_file('images/iconito.png')
        window.set_title("Super Comparador de Directorios")
        window.connect("delete_event", self.delete)
        # FIXME: hacer que recuerde!!
        window.resize(600,400)

        # necesitamos una caja vertical
        mainvbox = gtk.VBox()
        window.add(mainvbox)

        # Instancia del UIManager
        uimanager = gtk.UIManager()
        accelgroup = uimanager.get_accel_group()
        window.add_accel_group(accelgroup)
        actiongroup = gtk.ActionGroup('UIManager')

        # Las acciones
        actiongroup.add_actions([
            # xml_name, icon, real_menu_text, accelerator, tooltip, callback
            ('NuevaComp', gtk.STOCK_OPEN, u'_Nueva comparación', None, 'Comparar dos directorios nuevos', self.newComparison),
            ('Refrescar', gtk.STOCK_REFRESH, '_Refrescar', None, 'Recomparar los directorios actuales', self.chilla),
            ('Salir', gtk.STOCK_QUIT, '_Salir', None, 'Sale del programa', self.delete),
            ('MainMenu', None, '_Principal'),

            ('Ayuda', gtk.STOCK_HELP, '_Ayuda', None, 'Ayuda sobre el programa', self.chilla),
            ('AcercaDe', gtk.STOCK_ABOUT, '_Acerca de...', None, 'Info acerca del programa', self.chilla),
            ('AyudaMenu', None, '_Ayuda'),
        ])

        # Agregamos menu y toolbar
        uimanager.insert_action_group(actiongroup, 0)
        uimanager.add_ui_from_string(ui_menu)
        menubar = uimanager.get_widget('/MenuBar')
        mainvbox.pack_start(menubar, False)
        toolbar = uimanager.get_widget('/Toolbar')
        mainvbox.pack_start(toolbar, False)

        # lo pcipal
        self.trees = tree.TreesPane()
        mainvbox.pack_start(self.trees)

        # status bar
        # FIXME: ponerle al final un contador de lo revisado y lo diferente
        self.sb = StatusBar()
        mainvbox.pack_start(self.sb, False)

        window.show_all()

    def chilla(self, widg):
        self.sb("FIXME!!")
        print "FIXME!!"

    def newComparison(self, action):
        # ask for the directories
        print "FIXME: ask for the directories!" # FIXME
        dir1 = "/tmp/testdirs/ccc"
        dir2 = "/tmp/testdirs/ddd"

        # compare them
        print "FIXME: get the real config options!" # FIXME
        compareOptions = dict(length=True)
        comp = comparer.TreeComparer(**compareOptions)
        info = comp.compare(dir1, dir2)
        self.tree.build(info)


if __name__ == "__main__":
    MainWindow()
    gtk.main()

